package com.pack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpgradeActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upgrade);
		dbAdapter=new DbAdapter(this);
		Bundle bundle=this.getIntent().getExtras();
		Button buttonModifica=(Button)findViewById(R.id.buttonModifica);
		Button buttonAnnulla=(Button)findViewById(R.id.buttonAnnulla);
		Button buttonElimina=(Button)findViewById(R.id.buttonElimina);	
		final EditText editTextNome=(EditText)findViewById(R.id.editTextNome);
		final EditText editTextCognome=(EditText)findViewById(R.id.editTextCognome);
		final EditText editTextEMail=(EditText)findViewById(R.id.editTextEmail);
		final EditText editTextNumero=(EditText)findViewById(R.id.editTextNumero);
		_id=bundle.getLong("_id");
		editTextNome.setText(bundle.getString("nome"));
		editTextCognome.setText(bundle.getString("cognome"));
		editTextEMail.setText(bundle.getString("email"));
		editTextNumero.setText(bundle.getString("numero"));	
		final Intent intentMain=new Intent(this, MainActivity.class);
		final Intent intentListView=new Intent(this, ListViewActivity.class);
		buttonModifica.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				dbAdapter.open();
				if (dbAdapter.updateContact(_id, editTextNome.getText().toString(),
						editTextCognome.getText().toString(),editTextEMail.getText().toString(),editTextNumero.getText().toString()))							
					mess="Modifica effettuata.";
				else
					mess="Modifica non effettuata.";				
				Toast.makeText(UpgradeActivity.this, mess,
						Toast.LENGTH_SHORT).show();
				dbAdapter.close();
				startActivity(intentListView);
				
			}
		});
		buttonElimina.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {					
				dbAdapter.open();
				if (dbAdapter.deleteContact(_id))							
					mess="Contatto eliminato.";
				else
					mess="Contatto non eliminato.";				
				Toast.makeText(UpgradeActivity.this, mess,
						Toast.LENGTH_SHORT).show();
				dbAdapter.close();
				startActivity(intentListView);
				
			}
		});
		buttonAnnulla.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				startActivity(intentMain);
				
			}
		});
		
	}		

	private DbAdapter dbAdapter;
	private long _id;
	private String mess="";	
}
